package org.jackysoft.util;


import org.jackysoft.query.RunnerQuery;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public class Page implements Cloneable {

	// 当前页码
	protected Integer page = 0;

	// 总页数
	protected Integer total = 0;

	// 总记录数
	protected Integer records = 0;

	// 每页条数
	protected Integer offset = 10;

	protected boolean ajaxable = true;
	
	protected String contextPath="";
	// 记录实体
	protected Iterable<?> rows;

	private String requestURI;
	// //////////////////////////////////////////////////////////	

	protected int from = 0;

	protected RunnerQuery  additionalQuery = new RunnerQuery();
		
	public static final Page build(int page,int records,Iterable<?> rows){
		Page p = new Page();
		p.setPage(page);
		p.setRecords(records);
		p.setRows(rows);
		return p;
	}
	
	public static final Page build(int page,int records,Iterable<?> rows,boolean ajaxable){
		Page p = build(page,records,rows);
		p.setAjaxable(ajaxable);
		return p;
	}
	
	public int getEnd() {
		if (page == (total - 1)) {
			// 最后一页
			int lst = records % offset;
			return getFrom() + lst;
		}
		return getFrom() + offset;
	}
	
	public int getFrom() {
		from = page * offset;
		return from < 0 ? 0 : from;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	
	/**
	 * @return 返回总页数
	 * 
	 * */
	public Integer getTotal() {
		return total;
	}

	public Integer getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
		double drecords = this.records.doubleValue();
		double doffset = this.offset.doubleValue();
		if (records > 0)
			total = new Double(Math.ceil(drecords / doffset)).intValue();
		else
			total = 0;
		if (page > total)
			page = new Double(total).intValue();
	}

	public Iterable getRows() {
		return rows;
	}

	public void setRows(Iterable rows) {
		this.rows = rows;
	}

	
	public Integer getOffset() {
		return offset;
	}

	public void setOffset(Integer offset) {
		this.offset = offset;
	}

	public enum NavType {
		prev, next, page, first, last, info
	}

	
	public Element getPaginationElement() {
		Document doc = Jsoup.parse("");
		Element div = doc.createElement("div");
		div.addClass("pagination");
		Element ul = doc.createElement("ul");
		div.appendChild(ul);
		Integer cpage = getPage();
		Integer total = getTotal();
		ul.appendChild(pager(0, false, doc, NavType.first));

		if (cpage >= 1) {
			// 可以显示上页按钮
			ul.appendChild(pager(cpage - 1, false, doc, NavType.prev));
		}
		// 默认显示10个页码
		Integer end = ((cpage + 10) >= total) ? total : (cpage + 10);

		// 如果当前页到最后一页数大于所要显示的页码个数10
		// 则按照顺序从当前页往后显示10个页码数
		for (int page = cpage; page < end; page++) {
			ul.appendChild(pager(page, (page == cpage), doc, NavType.page));
		}

		if (cpage < (total - 1)) {
			ul.appendChild(pager(cpage + 1, false, doc, NavType.next));
		}

		ul.appendChild(pager(getTotal() - 1, false, doc, NavType.last));
		ul.appendChild(pager(cpage, false, doc, NavType.info));

		return div;
	}

	
	public String getPagination() {

		return getPaginationElement().outerHtml();

	}

	/**
	 * @param page
	 *            页码
	 * @param actived
	 * 
	 * */
	Element pager(Integer page, boolean actived, Document doc, NavType navType) {

		page = page < 0 ? 0 : page;
		Element li = doc.createElement("li");
		Element a = doc.createElement("a");
		a.attr("page", page + "");
		if(ajaxable){
			a.attr("href", "javascript:void(0);");			
			a.attr("link", getAction(page));
		}else{
			a.attr("href", getAction(page));
			a.attr("target","_self");
		}
		if (actived) {
			li.addClass("active");
			a.addClass("noclick");
		}
		switch (navType) {
		case prev:
			a.text("上页");
			li.appendChild(a);
			break;
		case next:
			a.text("下页");
			li.appendChild(a);
			break;
		case page:
			a.text(page + 1 + "");

			li.appendChild(a);
			break;
		case first:
			a.text("首页");

			li.appendChild(a);
			break;
		case last:
			a.text("末页");
			li.appendChild(a);
			break;
		case info:
			String str = String.format(
					"第  %s/%s 页 , 共  %s 条记录,当前显示从 %s 到 %s 条", getPage() + 1,
					getTotal(), getRecords(), getFrom(), getEnd());
			Element span = li.appendElement("span");
			span.text(str);

			break;
		}

		return li;
	}

	@Override
	public Page clone() {

		Object obj = null;
		try {
			obj = super.clone();
		} catch (CloneNotSupportedException e) {

		}
		return (Page) obj;
	}
	
	public String getAction(int page) {
		String actionUrl = this.getRequestURI();
		
		String additional = this.additionalQuery.semanticHttpQuery();
		
		return contextPath+"/"+actionUrl+(!StringUtils.isEmpty(additional)?"&"+additional:"");
	}


	public void addParameter(String name,String value) {
	      this.additionalQuery.addField(name, value);
	}


	public boolean isAjaxable() {
		return ajaxable;
	}


	public void setAjaxable(boolean ajaxable) {
		this.ajaxable = ajaxable;
	}

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	public String getRequestURI() {
		return requestURI;
	}

	public void setRequestURI(String requestURI) {
		this.requestURI = requestURI;
	}

	

	
}
